<template>
  <div>
    <el-select v-if="roles.indexOf('admin') != -1" v-model="value" placeholder="请选择区" filterable @change="areaChange">
      <el-option
        v-for="item in area"
        :key="item.id"
        :label="item.value"
        :value="item.id">
      </el-option>
    </el-select>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { GetAreaList } from '../../api/select'
export default {
  data () {
    return {
      area: [{
        id: 0,
        value: '全部'
      }],
      value: ''
    }
  },
  mounted () {
    this.areaChange(0)
  },
  methods: {
    areaChange (selVal) {
      this.$store.dispatch('SetAreaId', selVal)
    }
  },
  computed: {
    ...mapGetters([
      'roles',
      'provinceId',
      'cityId',
      'areaId'
    ])
  },
  watch: {
    cityId: function (value) {
      if (value === 0) {
        this.area = [{ id: 0, value: '全部' }]
      } else {
        GetAreaList().then(res => {
          this.area = [{ id: 0, value: '全部' }].concat(res.data.area)
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
